package com.xbongbong.paas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.LinkListPojo;
import com.xbongbong.paas.help.CloudCodeHelp;
import com.xbongbong.paas.help.LinkListHelp;
import com.xbongbong.paas.lowcode.CustomButtonService;
import com.xbongbong.paas.pojo.DetailTabPojo;
import com.xbongbong.paas.pojo.WorkFlowPojo;
import com.xbongbong.paas.service.LinkListService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.custombutton.pojo.CustomButtonPojo;
import com.xbongbong.pro.custombutton.pojo.dto.CustomButtonListDTO;
import com.xbongbong.pro.custombutton.pojo.vo.CustomButtonListVO;
import com.xbongbong.pro.enums.lowcode.ButtonTypeEnum;
import com.xbongbong.pro.form.pojo.TopPermissionsPoJo;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/03/11 11:04
 */
@Service("linkListService")
public class LinkListServiceImpl implements LinkListService {

    @Resource
    private LinkListHelp linkListHelp;
    @Resource
    private CloudCodeHelp cloudCodeHelp;
    @Resource
    private CustomButtonService customButtonService;

    /**
     * 公共tab
     * @param corpid
     * @param loginUser
     * @param handlerExplainVO
     * @param businessType
     * @param del
     * @param linkList
     * @return java.util.List<com.xbongbong.paas.pojo.DetailTabPojo>
     * @author 吴峰
     * @date 2022/2/14 14:19
     * @throws XbbException
     */
    @Override
    public List<DetailTabPojo> commonTab(String corpid, UserVO loginUser, HandlerExplainVO handlerExplainVO, HandlerExplainDTO handlerExplainDTO, Integer businessType, Integer del, List<LinkListPojo> linkList) throws XbbException {
        List<DetailTabPojo> detailTabList;
        if ( !Objects.equals(XbbRefTypeEnum.WORK_REPORT.getCode(), businessType) ) {
            detailTabList = ExplainUtil.getFieldListIncludeSubForm(handlerExplainVO, handlerExplainDTO);
            if (Objects.equals(businessType, XbbRefTypeEnum.COMMUNICATE_PLAN.getCode())) {
                for (DetailTabPojo detailTabPojo : detailTabList) {
                    if (Objects.equals(detailTabPojo.getAttr(), CommunicatePlanEnum.CONTACT_WAY.getAttr())) {
                        // 这里需要将访客计划的联系方式字段的editHide置为0，bug：20848
                        detailTabPojo.getFieldAttrEntity().setEditHide(0);
                        break;
                    }
                }
            }
            Integer business = businessType;
            detailTabList.removeIf(item -> Objects.equals(business, XbbRefTypeEnum.PRODUCT.getCode()) && Objects.equals(item.getAttr(), ProductEnum.STOCK_NOTIFY.getAttr()));
        } else {
            // 获取子表单tab栏
            detailTabList = ExplainUtil.getSubFormToTabWorkReport(handlerExplainVO, handlerExplainDTO);
        }
        if ( Objects.equals(DelEnum.NORMAL.getDel(), del) ) {
            // 拼装关联列表的tab和按钮
            linkListHelp.getLinkList(linkList, corpid);
            linkListHelp.getLinkListTab(linkList, detailTabList, loginUser);
        }

        cloudCodeHelp.setCloudCodeTab4Web(corpid, loginUser.getDynamicPermSet(), handlerExplainVO.getAppId(), handlerExplainVO.getFormId(), businessType, detailTabList);

        return detailTabList;
    }

    @Override
    public List<WorkFlowPojo>  getCommonButton(List<LinkListPojo> linkList, Integer del, UserVO loginUser, Long appId, Long formId, Integer businessType, Integer saasMark, String corpid) throws XbbException {
        List<WorkFlowPojo> workFlowPojoList = new ArrayList<>();
        if ( Objects.equals(DelEnum.NORMAL.getDel(), del) ) {
            // 拼装关联列表的tab和按钮
            linkListHelp.getLinkList(linkList, corpid);
            workFlowPojoList.addAll(linkListHelp.getLinkListAddButton(linkList, loginUser, PlatFormEnum.WEB));
            // 低代码按钮
            workFlowPojoList.addAll(customButtonService.getButtonList(businessType, formId, saasMark, corpid, loginUser, ButtonTypeEnum.DETAIL.getCode()));
        }
        return workFlowPojoList;
    }

    @Override
    public List<TopPermissionsPoJo> getDingtalkCommonButton(List<LinkListPojo> linkList, Integer del, UserVO loginUser, Long appId, Long formId, Integer businessType, Integer saasMark, String corpid) throws XbbException {
        List<TopPermissionsPoJo> list = new ArrayList<>();
        // 设置关联列表的按钮
        linkListHelp.getLinkList(linkList, corpid);
        list.addAll(linkListHelp.setMobileLinkListButton(linkList, loginUser));
        cloudCodeHelp.setCloudCodeDetailButton4Dingtalk(corpid, loginUser.getDynamicPermSet(), appId,  formId, businessType, list);

        // 低代码按钮
        CustomButtonListDTO customButtonListDTO = new CustomButtonListDTO();
        customButtonListDTO.setBusinessType(businessType);
        customButtonListDTO.setFormId(formId);
        customButtonListDTO.setSaasMark(saasMark);
        customButtonListDTO.setCorpid(corpid);
        customButtonListDTO.setLoginUser(loginUser);
        customButtonListDTO.setType(ButtonTypeEnum.DETAIL.getCode());
        CustomButtonListVO formCustomButtonListVO = customButtonService.listByPermission(customButtonListDTO);
        if (Objects.nonNull(formCustomButtonListVO) && CollectionsUtil.isNotEmpty(formCustomButtonListVO.getCustomButtonList())) {
            for (CustomButtonPojo item : formCustomButtonListVO.getCustomButtonList()) {
                TopPermissionsPoJo buttonPojo = new TopPermissionsPoJo();
                buttonPojo.setActionList(item.getActionList());
                buttonPojo.setValue(item.getName());
                buttonPojo.setAttr(PaasConstant.LOW_CODE+ StringConstant.UNDER_LINE+item.getId());
                buttonPojo.setBtnType(PaasConstant.LOW_CODE);
                list.add(buttonPojo);
            }
        }
        return list;
    }
}
